//define
var  xmlhttp;
var  xmlDoc;
var  value = new Array();
var  variable = new Array();

//Get Request Data's length
function  getlen(str)
{
    var  bytesCount = 0;
    for (var  i = 0; i < str.length; i++)
    {
        var  c = str.charAt(i);
        
        if (/^[u0000-u00ff]$/.test(c))
        {
            bytesCount += 1;
        }
        else
        {
            bytesCount += 2;
        }
    }
    return bytesCount;
}

//Create XMLHttpRequest Object
function  createXMLHttpRequest()

{

    try{
        var browser=navigator.appName
        var b_version=navigator.appVersion
        var version=parseFloat(b_version)
        //var browser = navigator.appName;
        //navigator.userAgent.toUpperCase().indexOf("MSIE"); 
        if((navigator.userAgent.toLowerCase().indexOf("trident") > -1 && navigator.userAgent.toLowerCase().indexOf("rv") > -1) || (navigator.userAgent.toUpperCase().indexOf("MSIE")!=-1)){           //IE浏览器
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            xmlDoc = new ActiveXObject('Microsoft.XMLDOM');     //创建空的XML文档对象
        }
        else{                                                   //其它浏览器
            xmlhttp = new XMLHttpRequest();
            xmlDoc = document.implementation.createDocument("","",null); //创建空的XML文档对象
        }
    }
    catch(error){
        alert("error");
    }
}
//send Request By HTTP POST
function  RequestByPost(method, variable, value, callback) {
    var  url, _Namespace;
    url = "http://59.46.127.228:8080/AMSWebService/AMSWebService.asmx?";

    createXMLHttpRequest();

    var  data;
    data = '<?xml version="1.0" encoding="utf-16"?>';
    data = data + '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
    data = data + '<soap:Body>';
    data = data + '<' + method + ' xmlns="http://tempuri.org/">';

    //for (var  i = 0; i < variable.length; i++) {
        //data = data + '<' + variable[i] + '>' + value[i] + '</' + variable[i] + '>';
    //}

    for (var i = 0; i < variable.length; i++) {
        data = data + '<' + variable[i] + '>';
        if (value.length != 0)
            if (value[i].constructor != window.Array) {
                data += value[i]
            }
            else {
                for (var j = 0; j < value[i].length; j++) {
                    data += '<string>' + value[i][j] + '</string>';
                }
            }

        data += '</' + variable[i] + '>';
    }

    data = data + '</' + method + '>';
    data = data + '</soap:Body>';
    data = data + '</soap:Envelope>';

    try {

        xmlhttp.open("POST", url, false);
        xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
        xmlhttp.setRequestHeader("Content-Length", getlen(data));
        //xmlhttp.SetRequestHeader ("SOAPAction",_Namespace+method);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(data);
        xmlDoc.async = false;   
        //xmlDoc.load(xmlhttp.responseBody);                      //加载XML文档
        xmlDoc=xmlhttp.responseXML.documentElement
        callback(xmlDoc);

    }
    catch (e) {
        alert(e.name + ": " + e.message);
    }
}

function getWebServiceUrl(funStr) {
    return 'http://' + window.location.host + '/AMSWebService/AMSWebService.asmx/' + funStr
}
